package org.anachronos.jeep.probec.model;

import javax.lang.model.element.VariableElement;

import org.anachronos.jeep.probe.Arg;
import org.anachronos.jeep.probe.ProbeClass;
import org.anachronos.jeep.probe.ProbeMethod;
import org.anachronos.jeep.probe.ReturnArg;
import org.anachronos.jeep.probe.This;

public class ProbeArg {
	public enum Kind {
		RETURN, ARG, CLASS, METHOD, THIS
	}
	private final Kind kind;
	private final int index;
	
	public ProbeArg(final Kind kind) {
		this(kind, -1);
	}
	
	public ProbeArg(final Kind kind, final int index) {
		super();
		this.kind = kind;
		this.index = index;
	}
	
	public static ProbeArg create(final VariableElement parameterElement) {
		final ReturnArg returnArg = parameterElement.getAnnotation(ReturnArg.class);
		if (returnArg != null) {
			return new ProbeArg(Kind.RETURN);
		}
		final Arg arg = parameterElement.getAnnotation(Arg.class);
		if (arg != null) {
			return new ProbeArg(Kind.ARG, arg.value());
		}
		final ProbeClass probeClass = parameterElement.getAnnotation(ProbeClass.class);
		if (probeClass != null) {
			return new ProbeArg(Kind.CLASS);
		}
		final ProbeMethod probeMethod = parameterElement.getAnnotation(ProbeMethod.class);
		if (probeMethod != null) {
			return new ProbeArg(Kind.METHOD);
		}
		final This that = parameterElement.getAnnotation(This.class);
		if (that != null) {
			return new ProbeArg(Kind.THIS);
		}
		return null;
	}
	
	public Kind getKind() {
		return kind;
	}
	
	public int getIndex() {
		return index;
	}

	@Override
	public String toString() {
		return "ProbeArg [kind=" + kind + ", index=" + index + "]";
	}
}
